package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mvp.base.BaseDialogFragment;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_ui.login.activity.LoginThridActivity;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/20.
 */
public class SetLanguageDialogFragment extends BaseDialogFragment {

    @BindView(R.id.ll_setlanguage_china)
    LinearLayout ll_setlanguage_china;
    @BindView(R.id.ll_setlanguage_english)
    LinearLayout ll_setlanguage_english;
    @BindView(R.id.iv_setlanguage_english)
    ImageView iv_setlanguage_english;
    @BindView(R.id.iv_setlanguage_china)
    ImageView iv_setlanguage_china;
    private boolean isEnglish = false;
    private boolean isBackKey = true;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_setlanguage;
    }

    @Override
    protected void initView() {

        Locale locale = LocalManageUtil.getAppLanguageSelect();
        d("--->>>>>>>>---当前APP语言:" + locale.getLanguage());
        boolean isEng = locale == Locale.ENGLISH;
        iv_setlanguage_english.setVisibility(isEng ? View.VISIBLE : View.GONE);
        iv_setlanguage_china.setVisibility(!isEng ? View.VISIBLE : View.GONE);
    }

    private void startLoginActivity() {
        getContext().startActivity(new Intent(getContext(), LoginThridActivity.class));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (isBackKey) {
            Timber.d("--->>>>>>>>---isBackKey:true");
            startLoginActivity();
        }else{
            Timber.d("--->>>>>>>>---isBackKey:"+false);
        }
        super.onDismiss(dialog);
    }

    @Override
    @OnClick({
            R.id.ll_setlanguage_china,
            R.id.ll_setlanguage_english,
            R.id.tv_setlanguage_finish
    })
    public void onClick(View v) {
        switch (v.getId()) {
            /**
             * 中英文按钮
             */
            case R.id.ll_setlanguage_china:
            case R.id.ll_setlanguage_english:
                isEnglish = v.getId() == R.id.ll_setlanguage_english;
                submit();
                break;
            /**
             * 完成按钮
             */
            case R.id.tv_setlanguage_finish:
                submit();
                break;
            default:
                break;
        }

    }

    /**
     * 提交确认
     */
    private void submit() {

        GlobalValue.saveFirstSetLanguage(false);
        //准备设置的语言
        Locale preLocale = isEnglish ? Locale.ENGLISH : Locale.CHINA;
        //已设置的语言
        Locale locale = LocalManageUtil.getAppLanguageSelect();
        d("--->>>>>>>>---APP语言:" + locale.getLanguage());
        d("--->>>>>>>>---准备设置APP语言:" + preLocale.getLanguage());
        if (locale == preLocale) {//只有当设置的语言不等于APP本来的语言时,才更改语言
            dismiss();
            return;
        }
        isBackKey = false;
        LocalManageUtil.setAppLanguageSelect(isEnglish);
        LocalManageUtil.getInstance().setConfiguration();

        Intent intent = new Intent(getContext(), MainActivity.class);
        /**
         * 启动新的主页后直接跳转登录页面
         */
        intent.putExtra("isStartLogin", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        getContext().startActivity(intent);
        dismiss();

    }

    @Override
    protected boolean isCancelableOutside() {
        return false;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.7f);
    }
}
